package com.zjgeport.zjgdanger.Exception;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.soar.util.net.rmi.httpxml.HttpCallBack;
import com.soar.util.net.rmi.httpxml.HttpClient;
import com.soar.util.net.rmi.httpxml.HttpMethod;
import com.soar.util.net.rmi.httpxml.HttpResult;
import com.zjgeport.zjgdanger.CommonActivity;
import com.zjgeport.zjgdanger.LoginActivity;
import com.zjgeport.zjgdanger.R;
import com.zjgeport.zjgdanger.adapter.AppGridAdapter;
import com.zjgeport.zjgdanger.common.ParamCommon;
import com.zjgeport.zjgdanger.model.myApplication;

import java.util.ArrayList;
import java.util.HashMap;

public class ExceptionMainActivity extends CommonActivity {

    private  AppGridAdapter saItem;
    private  Integer illegalCount;
    private  Integer acciCount;
    private  GridView gv_one;
    private  ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exception_main);
        initCommonListener();
        initView();
    }

    private void initView() {
        illegalCount = 0;
        acciCount = 0;
        findTextViewById(R.id.title).setText("异常处理");
        String names01[] = { "违法异常处理", "事故异常处理"};
        int draws01[] = { R.drawable.btn_exception_item1, R.drawable.btn_exception_item2};
        gv_one = (GridView)findViewById(R.id.GridView_app);
        gv_one.setSelector(new ColorDrawable(Color.TRANSPARENT));
        for (int i = 0; i < names01.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", draws01[i]); // 为加载数据设置图标
            map.put("ItemText", names01[i]); // 设置其名称
            meumList.add(map);
        }

        getIllegalCount();

    }

    private void  getIllegalCount()
    {
        HttpMethod method = new HttpMethod();
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("getIllegalCount");
        method.setKvdata(map);
        HttpClient.postdata(new Button(ExceptionMainActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {
                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)){
                    HashMap<String,String> map = result.getKvData();
                    if (map != null && map.size() > 0){

                        // 刷新UI
                        Looper mainLooper = Looper.getMainLooper();
                        MyHandler handler = new MyHandler(mainLooper);
                        Message msg = new Message();
                        msg.what = 111;
                        msg.obj = map;
                        handler.sendMessage(msg);
                    }

                }else {
                    Toast.makeText(getApplication(), result.getResultMessage(), 1).show();
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(ExceptionMainActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }

            }
        });

    }

    private class MyHandler extends Handler
    {
        public MyHandler(Looper looper)
        {
            super(looper);
        }

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 111:
                    HashMap<String,String> map = (HashMap<String,String>)msg.obj;
                    String ILLEGALCOUNT = map.get("ILLEGALCOUNT").toString();
                    String ACCICOUNT = map.get("ACCICOUNT").toString();
                    illegalCount =  Integer.parseInt(ILLEGALCOUNT);
                    acciCount = Integer.parseInt(ACCICOUNT);
                    // 为适配器中数据设置布局样式
                    int widthScreen  = getWindow().getWindowManager().getDefaultDisplay().getWidth();
                    saItem = new AppGridAdapter(ExceptionMainActivity.this, meumList,widthScreen - 3*14,illegalCount,acciCount);
                    gv_one.setAdapter(saItem); // 为应用网格视图增加数据来源
                    gv_one.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Intent intent;
                            switch (i){
                                case 0:
                                    Intent intent1 = new Intent(ExceptionMainActivity.this, IllegalMainActivity.class);
                                    startActivity(intent1);
                                    break;
                                case 1:
                                    Intent intent2 = new Intent(ExceptionMainActivity.this, AccidentMainActivity.class);
                                    startActivity(intent2);
                                    break;
                                case 2:
                            }
                        }
                    });

                    break;

                default:
                    break;
            }
        }
    }
}



